package com.china08.yxyapi.api.manage.tec.teaching.lecture.prepare;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;

import io.swagger.annotations.ApiOperation;

/**
 * 教师课前准备-下载授课包-轮询下载状态接口
 * 
 */
@CrossOrigin
@RestController
public class LectureDelPrePackDownLoadController extends YxyApiBaseController {

    @Value("${yw.esbag.pack.tmp.dir}")
    private String userHome;

    @ApiOperation(value = "删除本地授课包下载文件")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/prepare/download/del", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, RespModel respModel,
            @RequestBody(required = false) ReqModel reqModel) throws Exception {
        String packId = reqModel.packId;
        String dataStr = reqModel.dataStr;
        // 用户Id
        String userId = getProfile(request, response).getId();
        // 文件状态存放位置
        String filePath = getUserHome() + "/" + ".YxYun-YxYunApiTplForYw" + "/" + dataStr + "/download/pack/" + userId
                + "/" + packId;
        // 删除
        try {
            Thread t = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.gc();
                        Thread.sleep(3000);
                        deleteDir(new File(filePath));
                    } catch (Exception e) {
                    } finally {
                        try {
                            deleteDir(new File(filePath));
                        } catch (Exception e2) {
                        }
                    }
                }
            });
            t.start();
        } catch (Exception e) {
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String packId; // 授课资源包Id
        public String dataStr;
    }

    protected static class RespModel extends YxyApiRespModel {
    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     * 
     * @param dir
     *            将要删除的文件目录
     * @return boolean Returns "true" if all deletions were successful. If a
     *         deletion fails, the method stops attempting to delete and returns
     *         "false".
     */
    private void deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            // 递归删除目录中的子目录下
            for (int i = 0; i < children.length; i++) {
                deleteDir(new File(dir + "/" + children[i]));
            }
            boolean result = dir.delete();
            if (!result) {
                System.gc();
                dir.delete();
            }
        } else {
            boolean result = dir.delete();
            if (!result) {
                System.gc();
                dir.delete();
            }
        }
    }

    public String getUserHome() {
        return userHome;
    }

    public void setUserHome(String userHome) {
        this.userHome = userHome;
    }
}
